import { Context } from 'koa';
// import path from 'path';
// import bcrypt from 'bcryptjs';
// import jwt from 'jsonwebtoken';
// import dayjs from 'dayjs';
// import { Op } from 'sequelize';
// import { userType, pwdType, imgType, IuserInfoType, ILoginType } from '@/types';
// import { IuserTokenType } from '@/types/auth';
import { addFiles,queryFiles } from '@/business/service/equipment/equipmentUpload.service';
import errors from '@/app/err.type';
// import { createHash, formatHumpLineTransfer, pwdHash, removeSpecifyFile } from '@/business/utils';

// import env from '@/config/default';


// const { IMG_URL } = env();

// const {
//   userExisting,
//   userLoginError,
//   userDoesNotExist,
//   userRegisterError,
//   InvalidConnectionError,
//   userStatusErr,
//   sqlErr,
//   getUserInfoErr,
//   enteredPasswordsDiffer,
//   reviseErr,
//   updateAvatarErr,
//   uploadParamsErr,
// } = errors;

// 上传ini文件
export const uploadIniMid = async (ctx: Context, next: () => Promise<void>) => {
  // iniFiles是前端传的参数
  let fileArr = []
  const files = (ctx.request as any).files;
  if (!Array.isArray(files.iniFiles)) {
    fileArr.push(files.iniFiles)
  } else {
    fileArr.push(...files.iniFiles)
  }
  let filesArr2 = fileArr.map(item => {
    let {newFilename,originalFilename,filepath} = item
    return {filepath,new_filename:newFilename,original_filename:originalFilename}
  })
  const res = await addFiles(filesArr2)

  await next();

};
// 查询ini文件
export const queryIniMid = async (ctx: Context, next: () => Promise<void>) => {
  const res = await queryFiles()
  ctx.state.formatData = res;
  await next();
};

/* 
// fileArr
[
  PersistentFile {
    _events: [Object: null prototype] { error: [Function (anonymous)] },
    _eventsCount: 1,
    _maxListeners: undefined,
    lastModifiedDate: 2024-02-20T11:22:46.966Z,
    filepath: 'H:\\leno-admin-copy\\backend\\uploads\\e48bf41ab44dc7d14d2f30c00.ini',
    newFilename: 'e48bf41ab44dc7d14d2f30c00.ini',
    originalFilename: 'DMU2A2-1N-0.ini',
    mimetype: 'application/octet-stream',
    hashAlgorithm: false,
    size: 7119,
    _writeStream: WriteStream {
      fd: null,
      path: 'H:\\leno-admin-copy\\backend\\uploads\\e48bf41ab44dc7d14d2f30c00.ini',
      flags: 'w',
      mode: 438,
      start: undefined,
      pos: undefined,
      bytesWritten: 7119,
      closed: true,
      _writableState: [WritableState],
      _events: [Object: null prototype],
      _eventsCount: 1,
      _maxListeners: undefined,
      [Symbol(kFs)]: [Object],
      [Symbol(kIsPerformingIO)]: false,
      [Symbol(kCapture)]: false
    },
    hash: null,
    [Symbol(kCapture)]: false
  }
] 
*/